package com.shujia.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo07Union {
  def main(args: Array[String]): Unit = {
    /**
     * union：转换算子，将两个同类型的RDD合并得到一个新的RDD
     * 新RDD的分区数等于两个RDD分区数之和
     */

    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName(this.getClass.getSimpleName.replace("$", ""))
    val sc = new SparkContext(conf)

    val rdd01: RDD[Int] = sc.parallelize(List(1, 2, 3, 4, 5))
    val rdd02: RDD[Int] = sc.parallelize(List(4, 5, 6, 7, 8))
    println(s"rdd01的分区数：${rdd01.getNumPartitions}")
    println(s"rdd01的分区数：${rdd02.getNumPartitions}")


    val unionRDD: RDD[Int] = rdd01.union(rdd02)
    println(s"unionRDD的分区数：${unionRDD.getNumPartitions}")

    unionRDD.foreach(println)

  }

}
